//
//  SelectBrandView.m
//  AutoHome
//
//  Created by wqh on 15-5-12.
//  Copyright (c) 2015年 Wqh. All rights reserved.
//

#import "SelectBrandView.h"
#import "BrandCarModel.h"
#import "CarOperationModel.h"


#define kSelectBrandViewCellId @"SelectBrandViewCellId"

@interface SelectBrandView()

@property (nonatomic) NSMutableArray *dataArray;
@property (nonatomic) NSMutableArray *titleArray;
@property (nonatomic) UIImageView *formSelectImageView;

@property (nonatomic) NSInteger index;//记录是哪一父视图
@property (nonatomic) NSInteger  selectIndex; //记录选中的是哪一个

@end

@implementation SelectBrandView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)awakeFromNib {
    [self createTableView];
    [self addObserver];
    [self createFormSelectView];
}

- (void)createFormSelectView {
    self.formSelectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth-80, 60, 20, 15)];
    self.formSelectImageView.image = [UIImage imageNamed:@"forms_icon_select"];
    [self.tableView addSubview:self.formSelectImageView];
}



- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initData) name:@"updateSelectBrandView" object:nil];
}

- (void)createTableView {
    
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kSelectBrandViewCellId  ];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 44)];
    headerView.backgroundColor = [UIColor customGrayColor];
    self.tableView.tableHeaderView = headerView;
    
}

- (void)initData {
    
    if ([self.title isEqualToString:@"选择品牌"]) {
        if (self.dataArray.count!=0) {
            [self.dataArray removeAllObjects];
            
        }
        if (self.titleArray.count!=0) {
            [self.titleArray removeAllObjects];
        }
        
        self.titleArray = [NSMutableArray arrayWithArray:self.dataArr[0]];
        self.dataArray = [NSMutableArray arrayWithArray:self.dataArr[1]];
        
        self.titleLabel.text = self.title;
        [self.titleArray insertObject:@"" atIndex:0];
        [self.dataArray insertObject:@[@"不限"] atIndex:0];
    
        [self.tableView reloadData];
        
        NSInteger section = 0;
        NSInteger row = 0;
        for (int i = 0; i < self.dataArray.count; i++) {
            for (int j = 0; j < [self.dataArray[i] count]; j++) {
                if ([self.carOperation isEqualToString:@"不限"]) {
                    goto overLabel;
                }
                if (i!=0) {
                    BrandCarModel *model = self.dataArray[i][j];
                    if ([self.carOperation isEqualToString:model.name]) {
                        
                        section = i;
                        row = j;
                        goto overLabel;
                    }
                }
                
                
                
            }
        }
    overLabel:
        {
            
        }
        CGRect rect = self.formSelectImageView.frame;
        
        CGFloat height = 0;
        for (int i = 0; i < section; i++) {
            height += [self.dataArray[i] count]*44+22;
          
        }
        
        rect.origin.y = 60+height+row*44;
        [UIView animateWithDuration:0 animations:^{
            self.formSelectImageView.frame =rect;
        }];

        
    }else {
        
        self.titleLabel.text = self.title;
        if (self.dataArray.count!=0) {
            [self.dataArray removeAllObjects];
        }
        self.dataArray = [NSMutableArray arrayWithArray:self.dataArr];
        [self.tableView reloadData];
        
        NSInteger i = 0;
        for (CarOperationModel *model in self.dataArray) {
            if ([model.name isEqualToString:self.carOperation]) {
                self.selectIndex = i;
            }
            i++;
        }
        self.formSelectImageView.frame = CGRectMake(kScreenWidth-80, 60+44*self.selectIndex, 20, 15);
    }
    [self createIndex];
    
}

- (void)createIndex {
    NSArray *array = @[@"选择品牌",@"价格",@"级别",@"国别",@"变速箱",@"结构",@"排量",@"燃料",@"配置"];
    for (NSString *str in array) {
        if ([self.title isEqualToString:str]) {
            self.index   = [array indexOfObject:str];
        }
    }
    
}

//点击关闭按钮
- (IBAction)closeBtnClick:(UIButton *)sender {
    [UIView animateWithDuration:0.5 animations:^{
        self.frame= CGRectMake(kScreenWidth, 20, kScreenWidth-50, kScreenHeight-20);
        if (self.myBlock) {
            self.myBlock();
        }
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([self.title isEqualToString:@"选择品牌"]) {
        return self.dataArray.count;
    }else {
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if ([self.title isEqualToString:@"选择品牌"]) {
        return [self.dataArray[section] count];
    }else {
        return self.dataArray.count;
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kSelectBrandViewCellId forIndexPath:indexPath];

    cell.textLabel.font = [UIFont systemFontOfSize:15];
    if ([self.title isEqualToString:@"选择品牌"]) {
        
        if (indexPath.section == 0) {
            cell.textLabel.text = @"不限";
            return cell;
        }
        BrandCarModel *model = self.dataArray[indexPath.section][indexPath.row];
        cell.textLabel.text = model.name;
        return cell;
    }
    CarOperationModel *model = self.dataArray[indexPath.row];
    cell.textLabel.text = model.name;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.titleArray[section];
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.title isEqualToString:@"选择品牌"]) {
        if (self.brandNameBlock) {
            self.brandNameBlock(self.dataArray[indexPath.section][indexPath.row]);
        }
        if (indexPath.section != 0) {
            self.valueBlock(self.dataArray[indexPath.section][indexPath.row],self.index);
        }else {
            CarOperationModel *model = [[CarOperationModel alloc] init];
            model.name = @"不限";
            model.value = @"0";
            self.valueBlock(model,self.index);
        }
        
        //将对号移动
        CGRect rect = self.formSelectImageView.frame;
        CGFloat height = 0;
        for (int i = 0; i < indexPath.section; i++) {
            height += [self.dataArray[i] count]*44+22;
        }
        rect.origin.y = 60+height+(indexPath.row)*44;
        [UIView animateWithDuration:0 animations:^{
            self.formSelectImageView.frame =rect;
        }];
        
    }else {
        
        CarOperationModel *model = self.dataArray[indexPath.row];
        self.valueBlock(model,self.index);

        //将对号移动
        CGRect rect = self.formSelectImageView.frame;
        rect.origin.y = 60+indexPath.row*44;
        [UIView animateWithDuration:0 animations:^{
            self.formSelectImageView.frame = rect;
        }];
        
    }
    
    
    
    //将此页面也到右边
    [UIView animateWithDuration:0.5 animations:^{
        self.frame= CGRectMake(kScreenWidth, 20, kScreenWidth-50, kScreenHeight-20);
        if (self.myBlock) {
            self.myBlock();
        }
    }];
    
}







@end
